package com.welab.pandahelper.utils;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.welab.pandahelper.activity.HelperActivity;
import com.welab.pandahelper.bean.ExpandableListItem;
import com.welab.pandahelper.bean.FastJsonUtils;
import com.welab.pandahelper.bean.MyResponse;
import com.welab.pandahelper.bean.PandaGift;
import com.welab.pandahelper.config.MyConfig;
import com.welab.pandahelper.http.OkHttpHelper;
import com.welab.pandahelper.http.SpotsCallBack;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

import static com.welab.pandahelper.bean.GsonDoubleInteger.getGson;

public class taskUtils {

    private static Gson mGson = getGson();


    private static List giftList;

    public static boolean getTask(){

        return false;
    }



    public static boolean getPandaTask(Context context,PandaGift pandaGift, String token){
        String url = "https://www.oextool.com/api/task/get.do?apptoken=" + token + "&sortOrder=" + pandaGift.getSortOrder() + "&giftLevel=" + pandaGift.getGiftLevel() + "&giftId=" + pandaGift.getGiftId();
        try {
            Response response = OkHttpHelper.getOkHttpHelperInstance().get(url);
            if (response.isSuccessful()){
                String resultSrt = response.body().string();
//                MyResponse myResponse = mGson.fromJson(resultSrt,MyResponse.class);
                MyResponse myResponse = FastJsonUtils.jsonToObject(resultSrt,MyResponse.class);
                return myResponse.getSuccess();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    public static MyResponse getPandaTaskList(String token){
        String url = "https://www.oextool.com/api/orderList_on.do?giftId=&pageIndex=1&apptoken=" + token;
        try {
            Response response = OkHttpHelper.getOkHttpHelperInstance().get(url);
            if (response.isSuccessful()){
                String resultSrt = response.body().string();
//                MyResponse myResponse = mGson.fromJson(resultSrt,MyResponse.class);
                MyResponse myResponse = FastJsonUtils.jsonToObject(resultSrt,MyResponse.class);
                Log.e(MyConfig.APP_NAME,resultSrt);
                Log.e(MyConfig.APP_NAME,url);
                return myResponse;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static MyResponse getCatTask(String token, String id ,String goodId){
//        "1393873104310583298"
        String url = "https://h5.meowstarke.com/tao/sellerShopTask/addTaskOrder?id=" + id + "&goodsId=" + goodId;
        Map<String,String> tokenMap = new HashMap<String,String>();
        tokenMap.put("token",token);
        try {
            Response response =  httpUtils.get(url,tokenMap);
            if (response.isSuccessful()){
                String resultSrt = response.body().string();
//                MyResponse myResponse = mGson.fromJson(resultSrt,MyResponse.class);
                MyResponse myResponse = FastJsonUtils.jsonToObject(resultSrt,MyResponse.class);
                Log.e(MyConfig.APP_NAME,resultSrt);
                Log.e(MyConfig.APP_NAME,url);
                return myResponse;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;

    }

    public static MyResponse getCatTaskList(String token,String goodsId){
        String url = "https://h5.meowstarke.com/tao/sellerShopTask/appTaskPageList";
        try {
            Map<String,String> tokenMap = new HashMap<String,String>();
            tokenMap.put("token",token);
            Map<String,String> taskJsonMap = new HashMap<String,String>();
            taskJsonMap.put("page","0");
            taskJsonMap.put("size","10");
            taskJsonMap.put("taskType","1");
            taskJsonMap.put("shopPlatform","1");
            taskJsonMap.put("goodsId",goodsId);
            String taskJson = FastJsonUtils.objectToJson(taskJsonMap);
            Response response = httpUtils.post(url,taskJson,tokenMap);
            if (response.isSuccessful()){
                String resultSrt = response.body().string();
//                MyResponse myResponse = mGson.fromJson(resultSrt,MyResponse.class);
                MyResponse myResponse = FastJsonUtils.jsonToObject(resultSrt,MyResponse.class);
                Log.e(MyConfig.APP_NAME,resultSrt);
                return myResponse;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static ArrayList<ExpandableListItem> getCatGifts(String token){

        ArrayList<ExpandableListItem> list = new ArrayList<>();
        String url = "https://h5.meowstarke.com/tao/sellerGoodsInfo/appPageList?page=0&size=100&goodsType=1";
        try {
            Map<String,String> tokenMap = new HashMap<String,String>();
            tokenMap.put("token",token);
            Response response = httpUtils.get(url,tokenMap);
            if (response.isSuccessful()){
                String resultSrt = response.body().string();
//                MyResponse myResponse = mGson.fromJson(resultSrt,MyResponse.class);
                MyResponse myResponse = FastJsonUtils.jsonToObject(resultSrt,MyResponse.class);
                Log.e(MyConfig.APP_NAME,resultSrt);
                List<Map<String,Object>> giftList = (List<Map<String, Object>>) ((Map<String,Object>)myResponse.getData()).get("records");
                for (Map<String,Object> m:giftList
                     ) {
                    ExpandableListItem item = new ExpandableListItem();
                    item.setAppName(m.get("goodsName").toString());
                    item.setPckName(m.get("id").toString());
                    item.setSelected(false);
                    list.add(item);
                }
                return list;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
